package com.usian.controller;

import com.usian.feign.CartFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderController {

    @Autowired
    private CartFeign cartFeign;

    //@Autowired
    //private LoadBalancerClient loadBalancerClient;//使用ribbon提供的负载均衡器

    @RequestMapping("addOrder")
    public String addOrder(){
        //从购物车中查询用户要购买的商品   http：//localhost：8888/findCart
        //使用rebbion 负载均衡器，选择一个合适的cart_service
        //ServiceInstance cart_service = loadBalancerClient.choose("cart_service");//根据底层的负载均衡算法
        //String ip = cart_service.getHost();
        //int port = cart_service.getPort();
        //调一个http协议的接口
        //RestTemplate restTemplate = new RestTemplate(); //http接口的客户端
        //String result = restTemplate.getForObject("http://"+ip+":"+port+"/findCart", String.class);
        String result = cartFeign.findCart();
        return "下单成功了！！！"+result;
    }
}
